﻿using Project.Ycm20250401.Service._20250522.Domain.Application.Models;
using Project.Ycm20250401.Service._20250522.Domain.Entities;
using Project.Ycm20250401.Service._20250522.Domain.IRepository;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Project.Ycm20250401.Service._20250522.Domain.Application
{
    public class GoodsOrderService
    {
        private readonly IGoodsOrderRepository _goodsOrderRepository;
        public GoodsOrderService(IGoodsOrderRepository goodsOrderRepository)
        {
            _goodsOrderRepository = goodsOrderRepository;   
        }
        /// <summary>
        /// 入库单
        /// </summary>
        /// <param name="req"></param>
        /// <returns></returns>
        /// <exception cref="Exception"></exception>
        public async Task AddGoodsOrder(GoodsOrderInRequest req)
        {
            var goodsOrder = await _goodsOrderRepository.GetLastGoodsOrderByCarNo(req.CarNumber);
            if (goodsOrder!= null)
            {
                throw new Exception("此车辆车牌有误，请核实后再操作！");
            }
            goodsOrder=new GoodsOrderModel(Guid.NewGuid().ToString());
            goodsOrder.Inbound(req.CarNumber, req.GrossWeight, new Entities.GoodsClass(), req.InboundPerson);
            await _goodsOrderRepository.InsertGoodsOrder(goodsOrder);
        }

        /// <summary>
        /// 出库单
        /// </summary>
        /// <param name="req"></param>
        /// <returns></returns>
        /// <exception cref="Exception"></exception>
        public async Task OutGoodsOrder(GoodsOrderOutRequest req)
        {
            var goodsOrder = await _goodsOrderRepository.GetGoodsOrderByOrderNo(req.CarNumber);
            if (goodsOrder == null)
            {
                throw new Exception("此车辆车牌有误，请核实后再操作！");
            }
            goodsOrder.Outbound(req.NetWeight, req.OutboundPerson);
            await _goodsOrderRepository.UpdateGoodsOrder(goodsOrder);
        }

        /// <summary>
        /// 查询入库单
        /// </summary>
        /// <param name="orderNo"></param>
        /// <returns></returns>
        public async Task<GoodsOrderResponse?> GetGoodsOrderByOrderNo(string orderNo)
        {
            var goodsOrder= await _goodsOrderRepository.GetGoodsOrderByOrderNo(orderNo);
            if (goodsOrder == null)
            {
                return null;
            }
            var response = new GoodsOrderResponse();
            response.CarNumber = goodsOrder.OrderNo;
            response.CarNumber = goodsOrder.CarNumber;
            response.GrossWeight = goodsOrder.GrossWeight;
            response.NetWeight = goodsOrder.NetWeight;
            response.InboundPerson = goodsOrder.InboundPerson;
            response.OutboundPerson = goodsOrder.OutboundPerson;
            response.State = goodsOrder.State;
            return response;
        }
    }
}
